package com.qx.controller;

import com.qx.dao.DepartmentDao;
import com.qx.dao.EmploryeeDao;
import com.qx.pojo.Department;
import com.qx.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmploryeeDao emploryeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<Employee> employees = emploryeeDao.getall();
        model.addAttribute("emp", employees);
        return "emp/list";
    }


    @GetMapping("/add")
    public String add(Model model) {
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";//返回到添加员工页面
    }

    @PostMapping("/add")
    public String addemp(Employee employee) {
        System.out.println(employee);
        emploryeeDao.add(employee);
        return "redirect:/emps";//返回到添加员工页面
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id, Model model) {
        Employee getallbyid = emploryeeDao.getallbyid(id);
        model.addAttribute("getallbyid", getallbyid);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/update";
    }
    @PostMapping("/update")
    public String updateemp(Employee employee){
        emploryeeDao.add(employee);
        return "redirect:/emps";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        emploryeeDao.del(id);
        return "redirect:/emps";
    }


}
